<template>
  <AMISRenderer :schema="schema" />
</template>

<script setup>
import AMISRenderer from '@/components/AMISRenderer.vue'
import roleMenu from './roleMenu'
import user from './user'

const schema = {
  type: 'page',
  //解决权限授权弹窗树形控件无法设置高度问题
  css:{
    '.amis-scope .antd-TreeControl > .antd-Tree ':{
      'height': '350px !important',
      'max-height': '350px !important'
    }
  },
  body: {
    type: 'crud',
    api: '/upms/admin/role/list',
    syncLocation: false,
    autoFillHeight: true,
    footerToolbar: [
      'statistics',
      'switch-per-page',
      'pagination'
    ],
    autoGenerateFilter: {
      showBtnToolbar: false,
      columnsNum: 3
    },
    headerToolbar:[
      {
        type: 'button',
        actionType: 'dialog',
        align: 'right',
        label: '新增',
        icon: 'fa fa-plus pull-left',
        primary: true,
        messages: {
          //不展示表单失败信息
          validateFailed: ""
        },
        dialog: {
          title: '新增角色',
          closeOnEsc: true,
          showErrorMsg: false,
          body: {
            type: 'form',
            name: 'role-add-form',
            api: 'post:/upms/admin/role/add',
            canAccessSuperData: false,
            messages:{
              validateFailed:''
            },
            body: [
              {
                type: 'input-text',
                name: 'roleName',
                label: '角色名称',
                maxLength: 50,
                required: true,
                validationErrors: {
                  isRequired: '请输入角色名称'
                }
              },
              {
                type: 'checkboxes',
                name: 'assignUserType',
                label: '默认用户类型',
                source: '/base/dict/options/user-type'
              },
              {
                type: 'input-text',
                name: 'remark',
                label: '备注',
                maxLength: 100,
              }
            ]
          }
        }
      }
    ],
    columns: [
      {
        name: 'id',
        label: '角色Id',
      },
      {
        name: 'roleName',
        label: '角色名称',
        searchable: {
          type: 'input-text',
          name: 'roleName',
          label: '角色名称',
          clearable: true
        }
      },
      {
        name: 'assignUserTypeStr',
        label: '默认用户类型'
      },
      {
        name: 'remark',
        label: '备注'
      },
      {
        type: 'operation',
        label: '操作',
        buttons: [
          {
            label: '分配权限',
            type: 'button',
            level: 'primary',
            actionType: 'dialog',
            dialog: {
              title: '分配菜单权限-${roleName}',
              body: roleMenu,
              closeOnEsc: true,
              showErrorMsg: false,
              size: 'md',
              style: {
                height: '558px'
              }
            }
          },
          {
            label: '分配用户',
            type: 'button',
            level: 'primary',
            actionType: 'dialog',
            dialog: {
              title: '分配用户-${roleName}',
              body: user,
              closeOnEsc: true,
              showErrorMsg: false,
              size: 'lg',
              actions:[
                {
                  type: 'button',
                  label: '返回',
                  actionType: 'close',
                  level: 'primary'
                }
              ]
            }
          },
          {
            label: '修改',
            type: 'button',
            actionType: 'dialog',
            dialog:{
              title: '修改角色',
              closeOnEsc: true,
              showErrorMsg: false,
              body:{
                type: 'form',
                api:'/upms/admin/role/update',
                messages:{
                  validateFailed:''
                },
                body:[
                  {
                    type: 'hidden',
                    name: 'id',
                  },
                  {
                    type: 'input-text',
                    name: 'roleName',
                    label: '角色名称',
                    maxLength: 50,
                    required: true,
                    validationErrors: {
                      isRequired: '请输入角色名称'
                    }
                  },
                  {
                    type: 'checkboxes',
                    name: 'assignUserType',
                    label: '默认用户类型',
                    source: '/base/dict/options/user-type'
                  },
                  {
                    type: 'input-text',
                    name: 'remark',
                    label: '备注',
                    maxLength: 100,
                  },
                ]
              }
            }
          },
          {
            label: '删除',
            type: 'button',
            level: 'danger',
            actionType:'ajax',
            confirmText: '确认要删除该记录？',
            api: {
              url: '/upms/admin/role/delete?id=${id}',
              method: 'post',
              data:{}
            },
          },
        ]
      }
    ]
  }
}

</script>
